﻿Option Explicit On
Option Strict On

Imports System.Text
Imports System.Data
Imports System.Data.SqlClient
Imports FRAMEWORK
Imports System.Drawing.Drawing2D

Public Class frm_JobOrder
    Inherits asWins.Forms.BaseForm
    Dim Da As New FRAMEWORK.SQLDataAccess
    Dim cuscode As String
    Dim jobIndex As String
#Region " Function and Procedure "
    Private Sub GetEmployeePrint()
        Dim dt As DataTable
        Dim para(13) As SqlParameter

        para(0) = New SqlParameter("@ACTION", 4)
        para(1) = New SqlParameter("@Employee_Code", "")
        para(2) = New SqlParameter("@Department_ID", 0)
        para(3) = New SqlParameter("@Employee_Title", "")
        para(4) = New SqlParameter("@Employee_Fname", "")
        para(5) = New SqlParameter("@Employee_Lname", "")
        para(6) = New SqlParameter("@Employee_Position", "")
        para(7) = New SqlParameter("@Employee_Sale", 0)
        para(8) = New SqlParameter("@Employee_StatusAccount", 0)
        para(9) = New SqlParameter("@Employee_level", 0)
        para(10) = New SqlParameter("@Employee_Status", 0)
        para(11) = New SqlParameter("@Create_User", "")
        para(12) = New SqlParameter("@Update_User", "")
        dt = Da.ExecuteDataset(CommandType.StoredProcedure, "sp_SYS_EMPLOYEE", para).Tables(0)

        If dt.Rows.Count > 0 Then
            cboEmpPrint.Items.Clear()
            cboEmpPrint.BeginUpdate()
            cboEmpPrint.DataSource = dt
            cboEmpPrint.DisplayMember = "FullName"
            cboEmpPrint.ValueMember = "Employee_Code"
            cboEmpPrint.EndUpdate()
        Else
            cboEmpPrint.DataSource = Nothing
        End If
        Me.cboEmpPrint.SelectedIndex = -1
    End Sub
    Private Sub GetComboStatus()
        Dim para(4) As SqlParameter
        para(0) = New SqlParameter("@ACTION", 3)
        para(1) = New SqlParameter("@ST_Code", "")
        para(2) = New SqlParameter("@ST_Name", "")
        para(3) = New SqlParameter("@ST_Remark", "")
        para(4) = New SqlParameter("@ST_Status", "")
        Dim dt As DataTable
        dt = Da.ExecuteDataset(CommandType.StoredProcedure, "sp_SYS_STATUS", para).Tables(0)
        If dt.Rows.Count > 0 Then
            cboStatus.BeginUpdate()
            cboStatus.DataSource = dt
            cboStatus.DisplayMember = "ST_Name"
            cboStatus.ValueMember = "ST_Code"
            cboStatus.EndUpdate()
        Else
            cboStatus.DataSource = Nothing
        End If
        cboStatus.SelectedIndex = -1
    End Sub
    Private Sub GetCombocboJobSize()
        Dim para(4) As SqlParameter
        para(0) = New SqlParameter("@ACTION", 3)
        para(1) = New SqlParameter("@WS_Code", "")
        para(2) = New SqlParameter("@WS_Name", "")
        para(3) = New SqlParameter("@WS_Remark", "")
        para(4) = New SqlParameter("@WS_Status", "")
        Dim dt As DataTable
        dt = Da.ExecuteDataset(CommandType.StoredProcedure, "sp_SYS_WORKSIZE", para).Tables(0)
        If dt.Rows.Count > 0 Then
            cboJobSize.BeginUpdate()
            cboJobSize.DataSource = dt
            cboJobSize.DisplayMember = "WS_Name"
            cboJobSize.ValueMember = "WS_Code"
            cboJobSize.EndUpdate()
        Else
            cboJobSize.DataSource = Nothing
        End If
        Me.cboJobSize.SelectedIndex = -1
    End Sub
    Private Sub GetCombocboPaperSize()
        Dim para(4) As SqlParameter
        para(0) = New SqlParameter("@ACTION", 3)
        para(1) = New SqlParameter("@PS_Code", "")
        para(2) = New SqlParameter("@PS_Name", "")
        para(3) = New SqlParameter("@PS_Remark", "")
        para(4) = New SqlParameter("@PS_Status", "")
        Dim dt As DataTable
        dt = Da.ExecuteDataset(CommandType.StoredProcedure, "sp_SYS_PAPERSIZE", para).Tables(0)
        If dt.Rows.Count > 0 Then
            cboPaperSize.DataSource = dt
            cboPaperSize.DisplayMember = "PS_Name"
            cboPaperSize.ValueMember = "PS_Code"
        Else
            cboPaperSize.DataSource = Nothing
        End If
        Me.cboPaperSize.SelectedIndex = -1
    End Sub
    Private Sub GetCombocboList()
        Dim para(4) As SqlParameter
        para(0) = New SqlParameter("@ACTION", 3)
        para(1) = New SqlParameter("@LT_Code", "")
        para(2) = New SqlParameter("@LT_Name", "")
        para(3) = New SqlParameter("@LT_Remark", "")
        para(4) = New SqlParameter("@LT_Status", "")
        Dim dt As DataTable
        dt = Da.ExecuteDataset(CommandType.StoredProcedure, "sp_SYS_LISTING", para).Tables(0)
        If dt.Rows.Count > 0 Then
            cboList.DataSource = dt
            cboList.DisplayMember = "LT_Name"
            cboList.ValueMember = "LT_Code"
        Else
            cboList.DataSource = Nothing
        End If
    End Sub
    Private Sub GetCombocboGram()
        Dim para(4) As SqlParameter
        para(0) = New SqlParameter("@ACTION", 3)
        para(1) = New SqlParameter("@GR_Code", "")
        para(2) = New SqlParameter("@GR_Name", "")
        para(3) = New SqlParameter("@GR_Remark", "")
        para(4) = New SqlParameter("@GR_Status", "")
        Dim dt As DataTable
        dt = Da.ExecuteDataset(CommandType.StoredProcedure, "sp_SYS_GRAM", para).Tables(0)
        If dt.Rows.Count > 0 Then
            cboGram.DataSource = dt
            cboGram.DisplayMember = "GR_Name"
            cboGram.ValueMember = "GR_Code"
        Else
            cboGram.DataSource = Nothing
        End If
    End Sub
    Private Sub GetCombocboPrint()
        Dim dt As DataTable

        Dim para(4) As SqlParameter

        para(0) = New SqlParameter("@ACTION", 3)
        para(1) = New SqlParameter("@PR_Code", "")
        para(2) = New SqlParameter("@PR_Name", "")
        para(3) = New SqlParameter("@PR_Remark", "")
        para(4) = New SqlParameter("@PR_Status", 0)
        dt = Da.ExecuteDataset(CommandType.StoredProcedure, "sp_SYS_PRINTSYSTEM", para).Tables(0)
        If dt.Rows.Count > 0 Then
            cboPrint.DataSource = dt
            cboPrint.DisplayMember = "PR_Name"
            cboPrint.ValueMember = "PR_Code"
        Else
            cboPrint.DataSource = Nothing
        End If
    End Sub
    'Private Sub GetCombocboColor()
    '    Dim para(5) As SqlParameter
    '    para(0) = New SqlParameter("@ACTION", 3)
    '    para(1) = New SqlParameter("@CL_Code", "")
    '    para(2) = New SqlParameter("@CL_Name", "")
    '    para(3) = New SqlParameter("@CL_Remark", "")
    '    para(4) = New SqlParameter("@CL_Status", "")
    '    Dim dt As DataTable
    '    dt = Da.ExecuteDataset(CommandType.StoredProcedure, "sp_SYS_COLOR", para).Tables(0)
    '    If dt.Rows.Count > 0 Then
    '        cboColor.DataSource = dt
    '        cboColor.DisplayMember = "CL_Name"
    '        cboColor.ValueMember = "CL_Code"
    '    Else
    '        cboColor.DataSource = Nothing
    '    End If
    'End Sub
    Private Sub GetCombocboProtect()
        Dim para(5) As SqlParameter
        para(0) = New SqlParameter("@ACTION", 3)
        para(1) = New SqlParameter("@LK_Code", "")
        para(2) = New SqlParameter("@LK_Name", "")
        para(3) = New SqlParameter("@LK_Remark", "")
        para(4) = New SqlParameter("@LK_Status", "")
        Dim dt As DataTable
        dt = Da.ExecuteDataset(CommandType.StoredProcedure, "sp_SYS_LACKER", para).Tables(0)
        If dt.Rows.Count > 0 Then
            cboProtect.Items.Clear()
            cboProtect.BeginUpdate()
            cboProtect.DataSource = dt
            cboProtect.DisplayMember = "LK_Name"
            cboProtect.ValueMember = "LK_Code"
            cboProtect.EndUpdate()
        Else
            cboProtect.DataSource = Nothing
        End If
        cboProtect.SelectedIndex = -1
    End Sub
    Private Sub GetCombocboPACKING()
        Dim para(5) As SqlParameter
        para(0) = New SqlParameter("@ACTION", 3)
        para(1) = New SqlParameter("@PK_Code", "")
        para(2) = New SqlParameter("@PK_Name", "")
        para(3) = New SqlParameter("@PK_Remark", "")
        para(4) = New SqlParameter("@PK_Status", "")
        Dim dt As DataTable
        dt = Da.ExecuteDataset(CommandType.StoredProcedure, "sp_SYS_PACKING", para).Tables(0)
        If dt.Rows.Count > 0 Then
            cboPacking.Items.Clear()
            cboPacking.BeginUpdate()
            cboPacking.DataSource = dt
            cboPacking.DisplayMember = "PK_Name"
            cboPacking.ValueMember = "PK_Code"
            cboPacking.EndUpdate()
        Else
            cboPacking.DataSource = Nothing
        End If
        cboPacking.SelectedIndex = -1
    End Sub
    Private Sub GetCombocboPaperType()
        Dim para(5) As SqlParameter
        para(0) = New SqlParameter("@ACTION", 3)
        para(1) = New SqlParameter("@PT_Code", "")
        para(2) = New SqlParameter("@PT_Name", "")
        para(3) = New SqlParameter("@PT_Remark", "")
        para(4) = New SqlParameter("@PT_Status", "")
        Dim dt As DataTable
        dt = Da.ExecuteDataset(CommandType.StoredProcedure, "sp_SYS_PAPERTYPE", para).Tables(0)
        If dt.Rows.Count > 0 Then
            cboPaperType.DataSource = dt
            cboPaperType.DisplayMember = "PT_Name"
            cboPaperType.ValueMember = "PT_Code"
        Else
            cboPaperType.DataSource = Nothing
        End If
    End Sub

    Private Sub LoadOrder()
        Dim dt As DataTable
        Dim para(1) As SqlParameter
        para(0) = New SqlParameter("@Con", 4)
        para(1) = New SqlParameter("@Search", txtJobCode.Text)
        dt = Da.ExecuteDataset(CommandType.StoredProcedure, "sp_Order_GetOrderSearch", para).Tables(0)
        If dt.Rows.Count > 0 Then
            GetDetail(dt)
            btnAdd.Enabled = False
        End If
    End Sub
    Private Sub GetDetail(ByVal dt As DataTable)
        Dim dr As DataRow
        For Each dr In dt.Rows
            'txtJobCode.Text = dr!JobOrder_Code.ToString()
            cboStatus.SelectedValue = dr!ST_Code
            cuscode = dr!Customer_Code.ToString()
            txtOwenJob.Text = dr!Customer_Name_1.ToString()
            txtContact.Text = dr!Cust_ContactList_Name.ToString()
            txtAddress.Text = dr!Cust_ContactList_Address.ToString()
            txtTel.Text = dr!Cust_ContactList_Tel.ToString()
            txtMobile.Text = dr!Cust_ContactList_Mobile.ToString()
            txtJobName.Text = dr!JobOrder_Name.ToString()
            cboJobSize.SelectedValue = dr!WS_Code
            cboPaperSize.SelectedValue = dr!PS_Code
            cboProtect.SelectedValue = dr!LK_Code
            cboPacking.SelectedValue = dr!PK_Code
            dtArt.Value = CDate(dr!JobOrder_Art_Date)
            dtRecift.Value = CDate(dr!JobOrder_Receive_Date)
            txtSummary.Text = dr!JobOrder_Summary.ToString()
            txtM_Amount.Text = dr!JobOrder_M_Amount.ToString()
            txtM_Tax.Text = dr!JobOrder_M_Tax.ToString()
            txtM_Total.Text = dr!JobOrder_M_Total.ToString()
            txtM_Credit.Text = dr!JobOrder_M_Credit.ToString()
            txtM_Balance.Text = dr!JobOrder_M_Balance.ToString()
            cboTaxCode.Text = dr!JobOrder_TaxCode.ToString()
        Next
        GetDetailGrid(txtJobCode.Text)

    End Sub
    Private Sub GetDetailGrid(ByVal jobcode As String)
        dgvDetail.AutoGenerateColumns = False
        Dim dt As DataTable
        Dim para(2) As SqlParameter
        para(0) = New SqlParameter("@ACTION", 0)
        para(1) = New SqlParameter("@JobOrder_Code", txtJobCode.Text)
        dt = Da.ExecuteDataset(CommandType.StoredProcedure, "sp_Order_GetOrderDetail", para).Tables(0)
        dgvDetail.DataSource = dt
        Dim i As Integer = 0
        Dim item As DataRow
        For Each item In dt.Rows
            dgvDetail(1, i).Value = item("LT_Code").ToString()
            dgvDetail(2, i).Value = item("PT_Code").ToString()
            dgvDetail(3, i).Value = item("GR_Code").ToString()
            dgvDetail(4, i).Value = item("PR_Code").ToString()
            dgvDetail(5, i).Value = item("CL_Code").ToString()
            i = i + 1
        Next


    End Sub
    Private Sub cmdCustomer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCustomer.Click
        If Me.grCustomer.Visible = False Then

            Me.grCustomer.Visible = True
            LoadCusAddress()
        Else
            Me.grCustomer.Visible = False
            cboCustCondition.SelectedIndex = -1
            txtCustSearch.Clear()
        End If
    End Sub
    Private Sub LoadCusAddress()
        Dim dt As DataTable

        Dim para(15) As SqlParameter

        para(0) = New SqlParameter("@ACTION", 7)
        para(1) = New SqlParameter("@Customer_Code", "")
        para(2) = New SqlParameter("@Employee_Code", "")
        para(3) = New SqlParameter("@Customer_Type", 0)
        para(4) = New SqlParameter("@Customer_Title", "")
        para(5) = New SqlParameter("@Customer_Name_1", "")
        para(6) = New SqlParameter("@Customer_Name_2", "")
        para(7) = New SqlParameter("@Customer_Dest", "")
        para(8) = New SqlParameter("@Customer_Status", 0)
        para(9) = New SqlParameter("@Create_User", FRAMEWORK.Framework.F_Username_Str)
        para(10) = New SqlParameter("@Create_Date", "")
        para(11) = New SqlParameter("@Update_User", FRAMEWORK.Framework.F_Username_Str)
        para(12) = New SqlParameter("@Update_Date", 0)
        para(13) = New SqlParameter("@Search", "")
        para(14) = New SqlParameter("@Con", 0)
        dt = Da.ExecuteDataset(CommandType.StoredProcedure, "sp_SYS_CUSTOMER", para).Tables(0)
        If dt.Rows.Count > 0 Then
            dgvCustAddress.DataSource = dt
        Else
            dgvCustAddress.DataSource = Nothing
        End If


    End Sub
    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        Dim lgb As New LinearGradientBrush(ClientRectangle, Color.Honeydew, Color.Aquamarine, LinearGradientMode.Vertical)
        e.Graphics.FillRectangle(lgb, ClientRectangle)

        lgb.Dispose()
    End Sub
    Private Sub Handler()
        ControlGotFocus(Me, Me)
        ControlLostFocus(Me, Me)
    End Sub
    Private Sub ControlGotFocus(ByVal parentCtr As Control, Optional ByVal ignoreControl As Control = Nothing)
        Dim ctr As Control
        For Each ctr In parentCtr.Controls
            If Not (ctr Is ignoreControl) Then
                If TypeOf ctr Is TextBox Then
                    AddHandler ctr.GotFocus, AddressOf EvenHand.KeyEventHand.TextBox_GotFocus
                ElseIf TypeOf ctr Is ComboBox Then
                    AddHandler DirectCast(ctr, ComboBox).GotFocus, AddressOf EvenHand.KeyEventHand.ComboBox_GotFocus
                End If
            End If
            ControlGotFocus(ctr, ignoreControl)
        Next
    End Sub
    Private Sub ControlLostFocus(ByVal parentCtr As Control, Optional ByVal ignoreControl As Control = Nothing)
        Dim ctr As Control
        For Each ctr In parentCtr.Controls
            If Not (ctr Is ignoreControl) Then
                If TypeOf ctr Is TextBox Then
                    AddHandler ctr.LostFocus, AddressOf EvenHand.KeyEventHand.TextBox_LostFocus
                ElseIf TypeOf ctr Is ComboBox Then
                    AddHandler DirectCast(ctr, ComboBox).LostFocus, AddressOf EvenHand.KeyEventHand.ComboBox_LostFocus
                End If
            End If
            ControlLostFocus(ctr, ignoreControl)
        Next
    End Sub
    Private Sub SearchCustomer()
        Dim dt As DataTable
        Dim para(14) As SqlParameter

        para(0) = New SqlParameter("@ACTION", 8)
        para(1) = New SqlParameter("@Customer_Code", "")
        para(2) = New SqlParameter("@Employee_Code", "")
        para(3) = New SqlParameter("@Customer_Type", 0)
        para(4) = New SqlParameter("@Customer_Title", "")
        para(5) = New SqlParameter("@Customer_Name_1", "")
        para(6) = New SqlParameter("@Customer_Name_2", "")
        para(7) = New SqlParameter("@Customer_Dest", "")
        para(8) = New SqlParameter("@Customer_Status", 0)
        para(9) = New SqlParameter("@Create_User", FRAMEWORK.Framework.F_Username_Str)
        para(10) = New SqlParameter("@Create_Date", "")
        para(11) = New SqlParameter("@Update_User", FRAMEWORK.Framework.F_Username_Str)
        para(12) = New SqlParameter("@Update_Date", 0)
        para(13) = New SqlParameter("@Search", txtCustSearch.Text)
        para(14) = New SqlParameter("@Con", cboCustCondition.SelectedIndex)
        dt = Da.ExecuteDataset(CommandType.StoredProcedure, "sp_SYS_CUSTOMER", para).Tables(0) 'เปลี่ยนเองเฟ้ย

        If dt.Rows.Count > 0 Then
            dgvCustAddress.DataSource = dt
        Else
            dgvCustAddress.DataSource = Nothing
        End If
    End Sub
#End Region
    Private Sub frm_JobOrder_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'getCombo("sp_SYS_STATUS", "ST_Name", "ST_Code", cboStatus)
        Handler()
        GroupBox1.BackColor = Color.FromArgb(0, 0, 0, 0)
        GroupBox2.BackColor = Color.FromArgb(0, 0, 0, 0)
        GroupBox3.BackColor = Color.FromArgb(0, 0, 0, 0)
        TabControl1.BackColor = Color.FromArgb(0, 0, 0, 0)
        GetEmployeePrint()
        GetCombocboJobSize() ' WORKSIZE
        GetCombocboPaperSize()
        GetCombocboList()
        GetCombocboGram()
        GetCombocboPrint()
        'GetCombocboColor()
        GetCombocboProtect() 'LACKER
        GetCombocboPACKING()
        GetCombocboPaperType()
        GetComboStatus()

        dgvCustAddress.AutoGenerateColumns = False
    End Sub

    

    Private Sub dgvCustAddress_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvCustAddress.CellDoubleClick
        Me.grCustomer.Visible = False
        txtOwenJob.Text = dgvCustAddress.Rows(e.RowIndex).Cells(1).Value.ToString
        txtAddress.Text = dgvCustAddress.Rows(e.RowIndex).Cells(2).Value.ToString
        txtContact.Text = dgvCustAddress.Rows(e.RowIndex).Cells(3).Value.ToString
        txtTel.Text = dgvCustAddress.Rows(e.RowIndex).Cells(4).Value.ToString
        cuscode = dgvCustAddress.Rows(e.RowIndex).Cells(0).Value.ToString
        txtMobile.Text = dgvCustAddress.Rows(e.RowIndex).Cells(5).Value.ToString
        cboCustCondition.SelectedIndex = -1
        txtCustSearch.Clear()
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        NewJob()
    End Sub
    Private Sub NewJob()
        If txtOwenJob.Text = "" Then
            MessageBox.Show("กรุณาระบุรายชื่อผู้ติดต่อ !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            txtOwenJob.Focus()
            Exit Sub
        End If
        Insert()

    End Sub
    Private Sub Insert()
        If MessageBox.Show("คุณต้องการเพิ่มข้อมูล ใช่หรือไม่?", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
            Try
                txtJobCode.Text = GetJobCode()
                Da.BeginTransaction()
                Dim para(26) As SqlParameter
                para(0) = New SqlParameter("@ACTION", 1)
                para(1) = New SqlParameter("@JobOrder_Code", txtJobCode.Text)
                para(2) = New SqlParameter("@JobOrder_Date", dtOrderDate.Value)
                para(3) = New SqlParameter("@JobOrder_Name", txtJobName.Text)
                para(4) = New SqlParameter("@Customer_Code", cuscode)
                para(5) = New SqlParameter("@WS_Code", cboJobSize.SelectedValue)
                para(6) = New SqlParameter("@PS_Code", cboPaperSize.SelectedValue)
                para(7) = New SqlParameter("@LK_Code", cboProtect.SelectedValue)
                para(8) = New SqlParameter("@PK_Code", cboPacking.SelectedValue)
                para(9) = New SqlParameter("@JobOrder_Description", txtDesc.Text)
                para(10) = New SqlParameter("@JobOrder_Art_Date", dtArt.Value)
                para(11) = New SqlParameter("@JobOrder_Receive_Date", dtRecift.Value)
                para(12) = New SqlParameter("@JobOrder_Summary", txtSummary.Text) ' จำนวนทั้งหมด
                para(13) = New SqlParameter("@JobOrder_TaxCode", cboTaxCode.Text)
                para(14) = New SqlParameter("@JobOrder_M_Amount", txtM_Amount.Text) ' จำนวนเงิน
                para(15) = New SqlParameter("@JobOrder_M_Tax", txtM_Tax.Text)
                para(16) = New SqlParameter("@JobOrder_M_Total", txtM_Total.Text)
                para(17) = New SqlParameter("@JobOrder_M_Credit", txtM_Credit.Text)
                para(18) = New SqlParameter("@JobOrder_M_Balance", txtM_Balance.Text)
                para(19) = New SqlParameter("@JobOrder_Remark", txtRemark.Text)
                para(20) = New SqlParameter("@ST_Code", cboStatus.SelectedValue)
                para(21) = New SqlParameter("@JobOrder_Is_Ready", 0)
                para(22) = New SqlParameter("@JobOrder_Status", 0)
                para(23) = New SqlParameter("@Create_User", FRAMEWORK.Framework.F_Username_Str)
                para(24) = New SqlParameter("@Create_Date", DateTime.Now)
                para(25) = New SqlParameter("@Update_User", FRAMEWORK.Framework.F_Username_Str)
                para(26) = New SqlParameter("@Update_Date", DateTime.Now)
                Dim result As Integer
                result = Da.ExecuteNonQuery(Da.Transaction, CommandType.StoredProcedure, "sp_ORDER", para)

                For i As Integer = 0 To dgvDetail.Rows.Count - 2
                    Dim paraDt(8) As SqlParameter

                    paraDt(0) = New SqlParameter("@ACTION", 0) ' insert
                    paraDt(1) = New SqlParameter("@JobOrder_Detail_RowOrder", dgvDetail.Rows.Item(i).Cells(0).FormattedValue.ToString) 'ส่งไปแต่ไม่ได้ใช้
                    paraDt(2) = New SqlParameter("@JobOrder_Code", txtJobCode.Text)
                    'If dgvDetail(1, i).Value.ToString() = Nothing Then
                    '    IsDBNull()
                    'End If
                    paraDt(3) = New SqlParameter("@LT_Code", IIf(IsDBNull(dgvDetail(1, i).Value), "", dgvDetail(1, i).Value))
                    paraDt(4) = New SqlParameter("@PT_Code", IIf(IsDBNull(dgvDetail(2, i).Value), "", dgvDetail(2, i).Value))
                    paraDt(5) = New SqlParameter("@GR_Code", IIf(IsDBNull(dgvDetail(3, i).Value), "", dgvDetail(3, i).Value))
                    paraDt(6) = New SqlParameter("@PR_Code", IIf(IsDBNull(dgvDetail(4, i).Value), "", dgvDetail(4, i).Value))
                    paraDt(7) = New SqlParameter("@CL_Code", (dgvDetail.Rows.Item(i).Cells(5).FormattedValue.ToString))
                    paraDt(8) = New SqlParameter("@JobOrder_Detail_Page", dgvDetail(6, i).Value.ToString())
                    result = Da.ExecuteNonQuery(Da.Transaction, CommandType.StoredProcedure, "sp_Order_InsertOrderDetail", paraDt)
                Next



                If result > 0 Then
                    Da.CommitTransaction()
                    MessageBox.Show("เพิ่มข้อมูลเรียบร้อยแล้ว !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    'Call ClearData(Me, Me)
                    'Call LoadCustomer()
                    'GetCusAddress(txtCode.Text)
                    'GetContact(txtCode.Text)
                    Me.Refresh()
                End If
                Call ClearData(Me, Me)

            Catch ex As Exception
                Da.RollBackTransaction()
                Exit Sub
            End Try
        End If
    End Sub
    Function GetJobCode() As String
        Dim str As String
        Dim dt As DataTable
        Dim para(1) As SqlParameter
        para(0) = New SqlParameter("@Con", 5)
        para(1) = New SqlParameter("@Search", txtSearchJob.Text)
        dt = Da.ExecuteDataset(CommandType.StoredProcedure, "sp_Order_GetOrderSearch", para).Tables(0)
        str = dt.Rows(0)(0).ToString()
        Return str

    End Function
    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        ClearData(Me, Me)
    End Sub
    Private Sub ClearData(ByVal parentCtr As Control, Optional ByVal ignoreControl As Control = Nothing)
        Dim ctr As Control
        For Each ctr In parentCtr.Controls
            If Not (ctr Is ignoreControl) Then
                If TypeOf ctr Is TextBox Then
                    ctr.Text = ""
                ElseIf TypeOf ctr Is ComboBox Then
                    DirectCast(ctr, ComboBox).SelectedIndex = -1
                ElseIf TypeOf ctr Is ListBox Then
                    CType(ctr, ListBox).Items.Clear()
                End If
            End If
            ClearData(ctr, ignoreControl)
        Next
        If dgvDetail.Rows.Count > 1 Then
            Dim dt As DataTable = DirectCast(dgvDetail.DataSource, DataTable)
            If dt Is Nothing Then
                dgvDetail.Rows.Clear()
            Else
                dt.Rows.Clear()
            End If
        End If
        btnAdd.Enabled = True
        btnEdit.Enabled = True
    End Sub

    Private Sub LoadAllOrder()
        Dim dt As DataTable
        dgvJob.AutoGenerateColumns = False
        Dim para(1) As SqlParameter
        para(0) = New SqlParameter("@Con", cboConJob.SelectedIndex)
        para(1) = New SqlParameter("@Search", txtSearchJob.Text)
        dt = Da.ExecuteDataset(CommandType.StoredProcedure, "sp_Order_GetOrderSearch", para).Tables(0)
        dgvJob.DataSource = dt
    End Sub
    Private Sub Edit()
        If MessageBox.Show("คุณต้องการแก้ไขข้อมูล ใช่หรือไม่?", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
            Try

                Da.BeginTransaction()
                Dim para(26) As SqlParameter
                para(0) = New SqlParameter("@ACTION", 3)
                para(1) = New SqlParameter("@JobOrder_Code", txtJobCode.Text)
                para(2) = New SqlParameter("@JobOrder_Date", dtOrderDate.Value)
                para(3) = New SqlParameter("@JobOrder_Name", txtJobName.Text)
                para(4) = New SqlParameter("@Customer_Code", cuscode)
                para(5) = New SqlParameter("@WS_Code", cboJobSize.SelectedValue)
                para(6) = New SqlParameter("@PS_Code", cboPaperSize.SelectedValue)
                para(7) = New SqlParameter("@LK_Code", cboProtect.SelectedValue)
                para(8) = New SqlParameter("@PK_Code", cboPacking.SelectedValue)
                para(9) = New SqlParameter("@JobOrder_Description", txtDesc.Text)
                para(10) = New SqlParameter("@JobOrder_Art_Date", dtArt.Value)
                para(11) = New SqlParameter("@JobOrder_Receive_Date", dtRecift.Value)
                para(12) = New SqlParameter("@JobOrder_Summary", txtSummary.Text) ' จำนวนทั้งหมด
                para(13) = New SqlParameter("@JobOrder_TaxCode", cboTaxCode.Text)
                para(14) = New SqlParameter("@JobOrder_M_Amount", txtM_Amount.Text) ' จำนวนเงิน
                para(15) = New SqlParameter("@JobOrder_M_Tax", txtM_Tax.Text)
                para(16) = New SqlParameter("@JobOrder_M_Total", txtM_Total.Text)
                para(17) = New SqlParameter("@JobOrder_M_Credit", txtM_Credit.Text)
                para(18) = New SqlParameter("@JobOrder_M_Balance", txtM_Balance.Text)
                para(19) = New SqlParameter("@JobOrder_Remark", txtRemark.Text)
                para(20) = New SqlParameter("@ST_Code", cboStatus.SelectedValue)
                para(21) = New SqlParameter("@JobOrder_Is_Ready", 0)
                para(22) = New SqlParameter("@JobOrder_Status", 0)
                para(23) = New SqlParameter("@Create_User", FRAMEWORK.Framework.F_Username_Str)
                para(24) = New SqlParameter("@Create_Date", DateTime.Now)
                para(25) = New SqlParameter("@Update_User", FRAMEWORK.Framework.F_Username_Str)
                para(26) = New SqlParameter("@Update_Date", DateTime.Now)
                Dim result As Integer
                result = Da.ExecuteNonQuery(Da.Transaction, CommandType.StoredProcedure, "sp_ORDER", para)

                For i As Integer = 0 To dgvDetail.Rows.Count - 2
                    Dim paraDt(8) As SqlParameter

                    If dgvDetail(0, i).Value.ToString() <> "" Then
                        paraDt(0) = New SqlParameter("@ACTION", 1)
                    Else
                        paraDt(0) = New SqlParameter("@ACTION", 0)
                    End If
                    paraDt(1) = New SqlParameter("@JobOrder_Detail_RowOrder", dgvDetail.Rows.Item(i).Cells(0).FormattedValue.ToString) 'ส่งไปแต่ไม่ได้ใช้
                    paraDt(2) = New SqlParameter("@JobOrder_Code", txtJobCode.Text)
                    paraDt(3) = New SqlParameter("@LT_Code", IIf(IsDBNull(dgvDetail(1, i).Value), "", dgvDetail(1, i).Value))
                    paraDt(4) = New SqlParameter("@PT_Code", IIf(IsDBNull(dgvDetail(2, i).Value), "", dgvDetail(2, i).Value))
                    paraDt(5) = New SqlParameter("@GR_Code", IIf(IsDBNull(dgvDetail(3, i).Value), "", dgvDetail(3, i).Value))
                    paraDt(6) = New SqlParameter("@PR_Code", IIf(IsDBNull(dgvDetail(4, i).Value), "", dgvDetail(4, i).Value))
                    paraDt(7) = New SqlParameter("@CL_Code", (dgvDetail.Rows.Item(i).Cells(5).FormattedValue.ToString))
                    paraDt(8) = New SqlParameter("@JobOrder_Detail_Page", dgvDetail(6, i).Value.ToString())
                    result = Da.ExecuteNonQuery(Da.Transaction, CommandType.StoredProcedure, "sp_Order_InsertOrderDetail", paraDt)
                Next



                If result > 0 Then
                    Da.CommitTransaction()
                    MessageBox.Show("เพิ่มข้อมูลเรียบร้อยแล้ว !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    'Call ClearData(Me, Me)
                    'Call LoadCustomer()
                    'GetCusAddress(txtCode.Text)
                    'GetContact(txtCode.Text)
                    Me.Refresh()
                End If
                Call ClearData(Me, Me)

            Catch ex As Exception
                Da.RollBackTransaction()
                Exit Sub
            End Try
        End If
    End Sub

    Private Sub cboConJob_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboConJob.SelectedIndexChanged
        txtSearchJob.Text = ""
    End Sub

    Private Sub txtSearchJob_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearchJob.TextChanged
        LoadAllOrder()
    End Sub

    Private Sub dgvJob_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvJob.CellDoubleClick
        If e.RowIndex = -1 Then
            Exit Sub
        End If
        Me.gbJobcode.Visible = False
        txtJobCode.Text = dgvJob.Rows(e.RowIndex).Cells(0).Value.ToString
        LoadOrder()
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click

    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        If txtOwenJob.Text = "" Then
            MessageBox.Show("กรุณาระบุรายชื่อผู้ติดต่อ !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            txtOwenJob.Focus()
            Exit Sub
        End If
        Edit()
    End Sub

    Private Sub txtTax_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboTaxCode.SelectedIndexChanged
        If txtM_Amount.Text = "" Then
            Exit Sub
        End If
        txtM_Tax.Text = ""
        GetMoney()
    End Sub
    Private Sub GetMoney()
        If txtM_Credit.Text = "" Then
            txtM_Credit.Text = "0.00"
        End If
        If txtM_Amount.Text <> "" Then
            If cboTaxCode.SelectedIndex = 0 Or cboTaxCode.Text = "NOVAT" Then
                txtM_Tax.Text = "0.00"
                txtM_Total.Text = (CDbl(txtM_Amount.Text) + CDbl(txtM_Tax.Text)).ToString("#,##0.00")
                txtM_Balance.Text = (CDbl(txtM_Amount.Text) - CDbl(txtM_Credit.Text)).ToString("#,##0.00")
            End If
            If cboTaxCode.SelectedIndex = 1 Or cboTaxCode.Text = "EX7" Then
                Dim x As Double = (CDbl(txtM_Amount.Text) * 7.0) / 100
                txtM_Tax.Text = x.ToString()
                txtM_Total.Text = txtM_Amount.Text.ToString()
                txtM_Balance.Text = (CDbl(txtM_Amount.Text) - CDbl(txtM_Credit.Text)).ToString("#,##0.00")
            End If
            If cboTaxCode.SelectedIndex = 2 Or cboTaxCode.Text = "IN7" Then
                Dim x As Double = (CDbl(txtM_Amount.Text) * 7.0) / 107
                txtM_Tax.Text = x.ToString("#,##0.00")
                txtM_Total.Text = ((CDbl(txtM_Amount.Text)) + x).ToString()
                If txtM_Credit.Text <> "" Then
                    txtM_Balance.Text = (CDbl(txtM_Total.Text) - CDbl(txtM_Credit.Text)).ToString("#,##0.00")
                End If
            End If
        End If
    End Sub
   
    Private Sub txtM_Total_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtM_Total.TextChanged
      

    End Sub

    Private Sub txtM_Credit_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtM_Credit.LostFocus
        If txtM_Credit.Text <> "" Then
            txtM_Balance.Text = (CDbl(txtM_Total.Text) - CDbl(txtM_Credit.Text)).ToString("#,##0.00")
        End If

    End Sub



    Private Sub txtM_Amount_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtM_Amount.GotFocus
       
    End Sub

    Private Sub txtM_Amount_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtM_Amount.LostFocus
        'If txtM_Credit.Text <> "" Then
        '    txtM_Total.Text = txtM_Amount.Text
        '    txtM_Balance.Text = (CDbl(txtM_Total.Text) - CDbl(txtM_Credit.Text)).ToString()
        'Else
        '    txtM_Balance.Text = (CDbl(txtM_Total.Text)).ToString()
        'End If
    End Sub

    Private Sub txtM_Balance_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtM_Balance.TextChanged
        'txtSummary.Text = txtM_Balance.Text
    End Sub

    Private Sub txtM_Total_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtM_Total.Validating
        If txtM_Credit.Text = "" Then
            Exit Sub
        End If
        If txtM_Amount.Text = "" Then
            Exit Sub
        End If
        If txtM_Credit.Text <> "" Or txtM_Amount.Text <> "" Then
            txtM_Balance.Text = (CDbl(txtM_Amount.Text) - CDbl(txtM_Credit.Text)).ToString("#,##0.00")

        End If
    End Sub

    Private Sub txtM_Amount_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtM_Amount.TextChanged
        txtM_Tax.Text = ""
        GetMoney()
    End Sub

    Private Sub txtM_Credit_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtM_Credit.TextChanged
        If txtM_Total.Text = "" Then
            txtM_Total.Text = "0.00"
        End If
        If txtM_Credit.Text = "" Then
            txtM_Credit.Text = "0.00"
        End If
        txtM_Balance.Text = (CDbl(txtM_Total.Text) - CDbl(txtM_Credit.Text)).ToString("#,##0.00")
    End Sub

    Private Sub cboCustCondition_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboCustCondition.SelectedIndexChanged
        Me.txtCustSearch.Focus()
    End Sub

    Private Sub txtCustSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtCustSearch.TextChanged
        If cboCustCondition.SelectedIndex <> -1 Then
            If txtCustSearch.Text <> "" Then
                SearchCustomer()
            End If
        End If     
    End Sub

    Private Sub cmdJobSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdJobSearch.Click
        cboConJob.SelectedIndex = 0
        LoadAllOrder()
        If Me.gbJobcode.Visible = False Then

            Me.gbJobcode.Visible = True

        Else
            Me.gbJobcode.Visible = False
        End If
    End Sub

    Private Sub btnCalMoney_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalMoney.Click
        Dim frmReciept As New frm_ReceiptMoney
        frmReciept.ShowDialog()
    End Sub

    Private Sub dgvDetail_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvDetail.CellClick

        If e.RowIndex = -1 Then
            Exit Sub
        End If
        If (dgvDetail.Rows(e.RowIndex).Cells(0).Value Is Nothing) Then
            jobIndex = ""
        Else
            jobIndex = dgvDetail.Rows(e.RowIndex).Cells(0).Value.ToString

        End If
        If e.ColumnIndex = 7 Then
            If dgvDetail.RowCount - 1 < 1 OrElse dgvDetail.SelectedCells(0).RowIndex = dgvDetail.RowCount - 1 Then
                Exit Sub
            End If

            If MessageBox.Show("คุณต้องการลบข้อมูล ใช่หรือไม่?", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
                If (jobIndex = "") Then
                    dgvDetail.Rows.Remove(dgvDetail.CurrentRow)
                    dgvDetail.Refresh()
                Else
                    DeleteJobDetail(CInt(jobIndex))
                    dgvDetail.RefreshEdit()
                End If
            Else
                Exit Sub
            End If
        End If
    End Sub

    Private Sub DeleteJobDetail(ByVal index As Integer)
        Dim paraDt(8) As SqlParameter
        Dim result As Integer
        Da.BeginTransaction()
 
        paraDt(0) = New SqlParameter("@ACTION", 2)
        paraDt(1) = New SqlParameter("@JobOrder_Detail_RowOrder", index)
        paraDt(2) = New SqlParameter("@JobOrder_Code", txtJobCode.Text)
        paraDt(3) = New SqlParameter("@LT_Code", " ")
        paraDt(4) = New SqlParameter("@PT_Code", " ")
        paraDt(5) = New SqlParameter("@GR_Code", " ")
        paraDt(6) = New SqlParameter("@PR_Code", " ")
        paraDt(7) = New SqlParameter("@CL_Code", " ")
        paraDt(8) = New SqlParameter("@JobOrder_Detail_Page", " ")
        result = Da.ExecuteNonQuery(Da.Transaction, CommandType.StoredProcedure, "sp_Order_InsertOrderDetail", paraDt)
        If result = 1 Then
            Da.CommitTransaction()
            MessageBox.Show("ลบข้อมูลเรียบร้อยแล้ว !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information)
            LoadOrder()
        End If


    End Sub
End Class